/**
 * 修复会议状态脚本
 * 将所有"进行中"的会议更新为"已结束"
 */

const Database = require('better-sqlite3');
const path = require('path');

// 数据库路径
const dbPath = path.join(__dirname, 'packages', 'server', 'data', 'database.sqlite');

console.log('📂 数据库路径:', dbPath);

try {
  // 打开数据库
  const db = new Database(dbPath);
  
  // 查询当前活动的会议
  const activeMeetings = db.prepare(`
    SELECT id, title, meeting_number, created_at 
    FROM meetings 
    WHERE status = 'active'
  `).all();
  
  console.log(`\n📊 找到 ${activeMeetings.length} 个"进行中"的会议:`);
  
  if (activeMeetings.length === 0) {
    console.log('✅ 没有需要更新的会议！');
    db.close();
    process.exit(0);
  }
  
  // 显示会议列表
  activeMeetings.forEach((meeting, index) => {
    console.log(`  ${index + 1}. ${meeting.title} (${meeting.meeting_number})`);
  });
  
  console.log('\n🔧 开始更新...');
  
  // 更新所有活动会议为已结束
  const result = db.prepare(`
    UPDATE meetings 
    SET status = 'ended', ended_at = ? 
    WHERE status = 'active'
  `).run(Date.now());
  
  console.log(`✅ 成功更新 ${result.changes} 个会议的状态为"已结束"`);
  
  // 验证更新
  const remainingActive = db.prepare(`
    SELECT COUNT(*) as count 
    FROM meetings 
    WHERE status = 'active'
  `).get();
  
  console.log(`\n📊 剩余"进行中"的会议: ${remainingActive.count}`);
  
  const endedMeetings = db.prepare(`
    SELECT COUNT(*) as count 
    FROM meetings 
    WHERE status = 'ended'
  `).get();
  
  console.log(`📊 "已结束"的会议: ${endedMeetings.count}`);
  
  // 关闭数据库
  db.close();
  
  console.log('\n🎉 完成！现在可以删除这些会议了。');
  console.log('💡 提示: 刷新浏览器的会议历史页面查看更新');
  
} catch (error) {
  console.error('❌ 错误:', error.message);
  process.exit(1);
}

